﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class RWGLNewTask : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                createTimeLabel.Text = DateTime.Now.ToString("yyyy-MM-dd");
                InitialUserList();
            }
        }

        private void InitialUserList()
        {
            UserService userservice = new UserService();
            List<User> users = new List<User>();
            users.Add(new User { UserID = 0, ContactPerson = "请选择任务工程师" });
            users.AddRange(((IQueryable<User>)(userservice.GetWorkingPeople())).ToList());
            userDropDownList.DataSource = users;
            userDropDownList.DataTextField = "ContactPerson";
            userDropDownList.DataValueField = "UserID";
            userDropDownList.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (userDropDownList.SelectedIndex == 0)
            {
                tipLabel.Text = "请选择任务工程师";
            }
            else if (string.IsNullOrEmpty(subjectTextBox.Text))
            {
                tipLabel.Text = "请填写任务主题";
            }
            else if (string.IsNullOrEmpty(contentTextBox.Text))
            {
                tipLabel.Text = "请填写任务内容";
            }
            else
            {
                UserService userservice = new UserService();
                TaskService taskservice = new TaskService();
                Task newtask = new Task();
                newtask.Content = contentTextBox.Text;
                newtask.CreateDate = DateTime.Now;
                newtask.ShouldCompleteDate = Convert.ToDateTime(shouldTextBox.Text);
                newtask.Status = "待处理";
                newtask.Subject = subjectTextBox.Text;
                newtask.User = userservice.GetUserByID(long.Parse(userDropDownList.SelectedValue));
                newtask.User1 = userservice.GetUserByID(ContextService.CurrentAccount.UserID);
                newtask.TaskType = TaskTypeRadioButtonList.SelectedValue;
                newtask.ReasonType = questionTypeDDL.SelectedValue;
                taskservice.InsertTask(newtask);

                // send email
                string emailtoassignee = string.Format(WebConfigurationManager.AppSettings["NewTaskToAssignee"], newtask.User.ContactPerson, newtask.User1.ContactPerson, newtask.Content);
                string emailtocreater = string.Format(WebConfigurationManager.AppSettings["NewTaskToCreater"], newtask.User1.ContactPerson, newtask.User.ContactPerson);

                EmailService.SendEmail(newtask.User.UserEmail, string.Format("{0}为您新建了一个任务，请及时处理", newtask.User1.ContactPerson), emailtoassignee);
                //EmailService.SendEmail(newtask.User1.UserEmail, "成功分配任务", emailtocreater);
                Reset();
                tipLabel.Text = "你成功的为" + newtask.User.ContactPerson + "分配了一个新任务";
            }
        }

        private void Reset()
        {
            contentTextBox.Text = "";
            subjectTextBox.Text = "";
            tipLabel.Text = "";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Reset();
        }
    }
}
